import router from '@/router';
import store from '@/store';

const whiteList = ['/login'];  // 路由白名单
/**
 * @param {*} to 要到哪里去
 * @param {*} from 你从哪里来
 * @param {*} next 跳转方法
 */
router.beforeEach(async (to,from,next)=>{
    if(store.getters.token){
        // 用户已登录 不允许进login
        if( to.path === '/login' ){
            next('/');
        }else{
            const data = await store.dispatch('user/getUserInfo');
            console.log(data);
            next();
        }
    }else{
        // 用户未登录 只允许进login
        if( to.path === '/login' || whiteList.indexOf(to.path) > -1 ){
            next();
        }else{
            next('/login');
        }
    }
});